from bs4 import BeautifulSoup

from com.arcfox.middleware import async_mysql_middleware as db
from com.arcfox.middleware import cos_middleware as cos
from com.arcfox.school.processor.base_processor import BaseProcessor


class SchoolDetailProcessor(BaseProcessor):

    def __init__(self):
        super().__init__()
        self.table = "tb_university_info"

    async def parse_and_store_school_detail(self, task, html, logo_response, data_version):
        soup = BeautifulSoup(html, "lxml")
        university_name = task['school_name']

        university_summary = soup.find("div", {"class": "jieshao"}).find("div", {"class": "info-detail"}).text \
            .replace("更多 >>", "")
        university_build_date = soup.find("p", {"class": "creation"}).text
        university_property = soup.find("p", {"class": "type"}).text
        university_category = soup.find("p", {"class": "classify"}).text
        university_belong = soup.find("p", {"class": "belong"}).text
        university_education = soup.find("p", {"class": "education"}).text
        university_city = soup.find("p", {"class": "cityName"}).text
        university_master_need_count = soup.find("p", {"class": "pointsOfShuo"}).text.replace("个", "")
        university_doctor_need_count = soup.find("p", {"class": "pointsOfBo"}).text.replace("个", "")
        university_province = soup.find("div", {"class": "layui-container info"}).find("p", {"class": "title"}).find(
            "span").text

        div_tag = soup.find("div", {"class": "gaikuang"}).find("div", {"class": "info-detail"})
        td_tags = div_tag.find_all("td")
        university_is_985 = td_tags[1].text
        university_is_211 = td_tags[3].text
        university_is_double_top_one = td_tags[5].text
        university_telephone = td_tags[15].text
        university_url = div_tag.find("a").attrs["href"]
        university_direction = td_tags[-1].text

        university_uid = self.generate_school_uid(university_name)
        university_img = university_uid + ".jpg"
        cos.upload_by_bytes(university_img, logo_response)
        mapping = db.MysqlMapping(self.table)
        data = {
            "university_name": university_name, "university_province": university_province,
            "university_uid": university_uid, "university_summary": university_summary,
            "university_build_date": university_build_date, "university_property": university_property,
            "university_category": university_category, "university_belong": university_belong,
            "university_education": university_education, "university_city": university_city,
            "university_master_need_count": int(university_master_need_count),
            "university_doctor_need_count": int(university_doctor_need_count),
            "university_is_985": 0 if university_is_985 == '否' else 1,
            "university_is_211": 0 if university_is_211 == '否' else 1,
            "university_is_double_top_one": 0 if university_is_double_top_one == '否' else 1,
            "university_telephone": university_telephone, "university_url": university_url,
            "university_direction": university_direction, "data_version": data_version,
            "university_img": university_img
        }
        condition = {"university_uid": university_uid}
        if await mapping.query(condition, ["university_uid"]):
            data['lastupdate_time'] = "now()"
            await mapping.update(data, condition)
        else:
            await mapping.insert(data)
